import { Component, OnInit, ViewChild } from '@angular/core';
import { ActivatedRoute, Router } from '@angular/router';
import { STComponent, STColumn, STRequestOptions, STChange } from '@delon/abc/st';
import { SFComponent, SFSchema, SFDateWidgetSchema } from '@delon/form';
import { NzModalService } from 'ng-zorro-antd/modal';
import { FreightAccountService } from '../../../services/freight-account.service';

@Component({
  selector: 'app-voucher-list',
  templateUrl: './voucher-list.component.html',
  styleUrls: ['../../../../commom/less/box.less']
})
export class VoucherListComponent implements OnInit {
  @ViewChild('st', { static: true })
  st!: STComponent;
  @ViewChild('sf', { static: false })
  sf!: SFComponent;
  @ViewChild('auditModal', { static: false })
  auditModal!: any;
  columns: STColumn[] = this.initST();
  searchSchema: SFSchema = this.initSF();

  _$expand = false;

  selectedRows: any[] = [];
  vc2code = null;
  constructor(public service: FreightAccountService, private route: ActivatedRoute, private router: Router) {
    this.vc2code = route.snapshot.params.id;
  }

  ngOnInit(): void {}

  beforeReq = (requestOptions: STRequestOptions) => {
    Object.assign(requestOptions.body, { vc2code: this.vc2code });
    if (this.sf) {
      Object.assign(requestOptions.body, {
        ...this.sf?.value
      });
      if (this.sf?.value.createTime) {
        Object.assign(requestOptions.body, {
          createTime: {
            start: this.sf?.value.createTime?.[0] || null,
            end: this.sf?.value.createTime?.[1] || null
          }
        });
      }
      if (this.sf?.value.vctime) {
        Object.assign(requestOptions.body, {
          vctime: {
            start: this.sf?.value.vctime?.[0] || null,
            end: this.sf?.value.vctime?.[1] || null
          }
        });
      }
    }
    return requestOptions;
  };

  stChange(e: STChange): void {
    switch (e.type) {
      case 'checkbox':
        this.selectedRows = e.checkbox!;
        break;
    }
  }

  /**
   * 重置表单
   */
  resetSF() {
    this.sf.reset();
    this._$expand = false;
  }

  /**
   * 伸缩查询条件
   */
  expandToggle() {
    this._$expand = !this._$expand;
    this.sf?.setValue('/expand', this._$expand);
  }

  private initSF(): SFSchema {
    return {
      properties: {
        expand: {
          type: 'boolean',
          ui: {
            hidden: true
          }
        },
        vccode: {
          type: 'string',
          title: '凭证号',
          ui: {
            autocomplete: 'off',
            placeholder: '请输入'
          }
        },
        vctime: {
          title: '凭证时间',
          type: 'string',
          ui: {
            widget: 'sl-from-to-search',
            format: 'yyyy-MM-dd'
          } as SFDateWidgetSchema
        },
        createTime: {
          title: '创建时间',
          type: 'string',
          ui: {
            widget: 'sl-from-to-search',
            format: 'yyyy-MM-dd'
          } as SFDateWidgetSchema
        },
        sourceCode: {
          type: 'string',
          title: '原始单号',
          ui: {
            autocomplete: 'off',
            placeholder: '请输入',
            visibleIf: {
              expand: (value: boolean) => value
            }
          }
        },
        sourceType: {
          type: 'string',
          title: '原始单类型',
          ui: {
            widget: 'select',
            placeholder: '请选择',
            visibleIf: {
              expand: (value: boolean) => value
            }
          }
        },
        drmoney: {
          type: 'string',
          title: '借方金额',
          ui: {
            placeholder: '请输入',
            visibleIf: {
              expand: (value: boolean) => value
            }
          }
        },
        inpinvcosde: {
          type: 'string',
          title: '凭证摘要',
          ui: {
            placeholder: '请输入',
            visibleIf: {
              expand: (value: boolean) => value
            }
          }
        },
        subid: {
          type: 'string',
          title: '科目',
          ui: {
            widget: 'dict-select',
            params: { dictKey: 'refund:apply:status' },
            placeholder: '请选择',
            visibleIf: {
              expand: (value: boolean) => value
            }
          }
        },
        crmoney: {
          type: 'string',
          title: '贷方金额',
          ui: {
            placeholder: '请输入',
            visibleIf: {
              expand: (value: boolean) => value
            }
          }
        }
      }
    };
  }

  private initST(): STColumn[] {
    return [
      { title: '凭证号', index: 'id', type: 'link', width: 150 },
      { title: '帐套', index: 'vcltdid', width: 150 },
      { title: '凭证时间', index: 'vctime', type: 'date', width: 150 },
      { title: '凭证类型', index: 'vctype', width: 150 },
      { title: '序号', index: 'invmoney', width: 150, format: _ => '1' },
      { title: '摘要', index: 'remarks', width: 300 },
      { title: '币种', index: 'currency', width: 100 },
      {
        title: '借方金额',
        index: 'drmoney',
        width: 120,
        type: 'widget',
        className: 'text-right',
        widget: { type: 'currency-chy', params: ({ record }) => ({ value: record.drmoney }) }
      },
      {
        title: '贷方金额',
        index: 'crmoney',
        width: 120,
        type: 'widget',
        className: 'text-right',
        widget: { type: 'currency-chy', params: ({ record }) => ({ value: record.crmoney }) }
      },
      { title: '凭证状态', index: 'stsLabel', width: 120 },
      { title: '创建时间', index: 'createTime', width: 140 },
      { title: '创建人', index: 'createUserId', width: 150 },
      {
        title: '操作',
        width: '130px',
        fixed: 'right',
        className: 'text-center',
        buttons: [
          {
            text: '浏览',
            click: (item: any) => this.router.navigate(['/financial-management/voucher-summary/list/detail/' + item.id])
          }
        ]
      }
    ];
  }
}
